from flask import request, Blueprint
from model.user import user_reg, user_login, addUserCollect, getUserCollect, delUserCollect, getCategoryNavlist, \
    user_add_background

user = Blueprint('user', __name__)


@user.route('/reg', methods=['GET', 'POST'])
def reg():
    if request.method == 'POST':
        data = request.get_json()
        userid = data['userid']
        password = data['password']
        username = data["username"]
        info = user_reg(userid, password, username)
        return info
    else:
        return "不支持GET方法"


@user.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        data = request.get_json()
        userid = data['userid']
        password = data['password']
        info = user_login(userid, password)
        return info
    else:
        return "不支持GET方法"


@user.route("/addUserCollect", methods=['POST', 'GET'])
def addCollect():
    if request.method == "POST":
        data = request.get_json()
        userid = data['userid']
        url = data['url']
        img = data["img"]
        navname = data['navname']
        token = data["token"]
        info = addUserCollect(userid, url, img, navname, token)
        return info
    else:
        return "不支持GET方法"


@user.route("/getUserCollect", methods=["POST", "GET"])
def getCollect():
    if request.method == "POST":
        return "不支持POST方法"
    else:
        userid = request.args.get("userid")
        token = request.args.get("token")
        info = getUserCollect(userid, token)
        return info


@user.route("/delUserCollect", methods=["POST", "GET"])
def delCollect():
    if request.method == "POST":
        data = request.get_json()
        userid = data['userid']
        navid = data['navid']
        token = data["token"]
        info = delUserCollect(userid, navid, token)
        return info
    else:
        return "不支持GET方法"


@user.route("/getcategorynavlist", methods=["POST", "GET"])
def getCategoryNav():
    if request.method == "POST":
        return "不支持POST方法"
    else:
        category = request.args.get("category")
        info = getCategoryNavlist(category)
        return info


@user.route("/useraddbackground", methods=["POST", "GET"])
def userAddBackground():
    if request.method == "POST":
        data = request.get_json()
        print(data)
        userid = data["userid"]
        token = data["token"]
        background = data["background"]
        info = user_add_background(userid, token, background)
        return info
    else:
        return "不支持GET方法"
